package doc.ic.walkmore.DAO;

import java.text.ParseException;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Pair;
import doc.ic.walkmore.DAO.model.Steps;
import doc.ic.walkmore.GUI.GUIManager;

public class StepsDataSourceWrittable extends StepsDataSource{
	  
	  // model UPDATES view
	  private GUIManager mGui;

	  public StepsDataSourceWrittable(Context context, GUIManager gui) {
	    super(context);
	    mGui = gui;
	  }
	  
	  @Override
	  public void open() throws SQLException {
		  super.open();
	    // On open of dtasource, update view
	    updateGUI(mGui.getDateToDisplay());
	  }

	  public Steps createSteps(Steps step) {
		  return createSteps(step.getNumber(), step.getTime(), step.getActivity(), step.getDistance(), step.getDate());
	  }
	  
	  public Steps createSteps(long number, long time, String activity, long distance, String date) {
	    ContentValues values = new ContentValues();
	    values.put(MySQLiteHelper.COLUMN_NUMBER, number);
	    values.put(MySQLiteHelper.COLUMN_TIME, time);
	    values.put(MySQLiteHelper.COLUMN_ACTIVITY, activity);
	    values.put(MySQLiteHelper.COLUMN_DISTANCE, distance);
	    values.put(MySQLiteHelper.COLUMN_DATE, date);
	    long insertId = database.insert(MySQLiteHelper.TABLE_STEPS, null,
	        values);
	    Cursor cursor = database.query(MySQLiteHelper.TABLE_STEPS,
	        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Steps newSteps = cursorToSteps(cursor);
	    cursor.close();
	    
	    // Try to updateGUi
	    updateGUI(date);
	    
	    return newSteps;
	  }

	  public void deleteSteps(Steps steps) {
	    long id = steps.getId();
	    System.out.println("Steps deleted with id: " + id);
	    database.delete(MySQLiteHelper.TABLE_STEPS, MySQLiteHelper.COLUMN_ID
	        + " = " + id, null);
	    updateGUI(steps.getDate());
	  }
	  
	  /**
	   * Updates gui if the date passed as parameter is the same as gui.getDateToDisplay() 
	   * @param date
	   * @return 0 if nothing was updated, 1 otherwise
	   */
	  public int updateGUI(String date) {
		    try {
			    String dataToDisplay = mGui.getDateToDisplay();
				if (compareDayinDate(dataToDisplay, date) == 0) {
					HashMap<String, Pair<Long, Long>> total = getTotalStepsBy(dataToDisplay);
					if (total.isEmpty())
						return 0;
					else {
						mGui.update(total);
						return 1;
					}
				} else {
					return 0;
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			return 0;
	  }
	  
	  private int compareDayinDate(String first, String second) throws ParseException {
		  return dayFormating.parse(first).compareTo(dayFormating.parse(second));
	  }
	  
	  /**
	   * Deletes entries of the DB of the specified date
	   * @param date
	   */
	  public void delete(String date) {
		  database.delete(MySQLiteHelper.TABLE_STEPS, "date("+MySQLiteHelper.COLUMN_DATE+") = date('"+date+"')", null);
		  updateGUI(date);
	  }

	  // These two  methods are just used to toggle the GUI's start/stop button 
	  // When it has been started automaticly
	public void warnStopSensing() {
		mGui.uncheckToggleButton();
	}

	public void warnStartSensing() {
		mGui.checkToggleButton();
	}
	} 